﻿<script src="~/Scripts/Highcharts-3.0.1/js/highcharts.js"></script>
@using FTBS.Models.Entities.Concert
@model List<FTBS.Models.Entities.Concert.ReportTube>
@{
    ViewBag.Title = "Thống kê";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    ViewBag.count = 0;
    //===========================================
    List<ReportTube> theatersData = ViewBag.TheaterData;
    List<ReportTube> cateData = ViewBag.CateRank;
    List<ReportTube> SystemBalance = ViewBag.SystemBalance;
    List<ReportTube> TheaterBalance = ViewBag.TheaterBalance;
    List<ReportTube> DayRank = ViewBag.DayRank;
    List<ReportTube> MonthRank = ViewBag.MonthRank;
    DateTime fDate = ViewBag.FDate;
    DateTime tDate = ViewBag.TDate;
    //==============for detail page=============
    string fTime = fDate.ToString("dd/MM/yyyy");
    string tTime = tDate.ToString("dd/MM/yyyy");
    //===========================================
    double totalBought = 0;
    double totalSold = 0;
    int totalBoughtNum = 0;
    int totalSoldNum = 0;
    double totalPriceBalance = 0;
    int totalNumberBalance = 0;
    
    double realBalance = 0;
    double realTotalRevenue = 0;
    double realTotalBalance = 0;
    //============================================
    List<ReportTube> TheaterSoldRank = new List<ReportTube>();
    Dictionary<string, double> TheaterSoldPriceRank = new Dictionary<string, double>();
    Dictionary<string, int> TheaterSoldNumberRank = new Dictionary<string, int>();

    Dictionary<string, int> TotalReturnTicket = new Dictionary<string, int>();
    Dictionary<string, int> TheatersReturnTicket = new Dictionary<string, int>();
    Dictionary<string, int> CategoriesRanking = new Dictionary<string, int>();
    //===========================================================================

    string theater = null;
    int tYear = 0;
    bool tAdd = false;
    ReportTube reportTube = new ReportTube();
    ReportTube tReportTube = new ReportTube();
    Dictionary<string, string> ListSoldSuppliersName = new Dictionary<string, string>();
    Dictionary<int, string> ListSoldTheaters = new Dictionary<int, string>();
}
<style>
    .filmkind {
        padding: 5px;
        font-size: 27px;
        background-color: gray;
        color: white;
    }

    .center {
        text-align: center;
    }

    .right {
        text-align: right;
    }
</style>
<div id="report">
    @*@((DotNet.Highcharts.Highcharts)ViewBag.AllChart)*@
    @if (Model.Count > 0)
    {
        <legend id="ReportTitle">THỐNG KÊ THEO THÁNG TỪ @fDate.ToString("dd/MM/yyyy") ĐẾN @tDate.ToString("dd/MM/yyyy")
        </legend>
        foreach (var bItem in Model)
        {
            double priceBalance = 0;
            int numberBalance = 0;
            if (!(theater == bItem.TheaterName && tYear == bItem.ReportDate.Month))
            {
                if (tAdd == true)
                {
                    // before create new tube, push the old tube to reportData
                    TheaterSoldRank.Add(tReportTube);
                    tReportTube = new ReportTube();
                }
                theater = bItem.TheaterName;
                tYear = bItem.ReportDate.Month;
                // create new tube
                tReportTube.ReportDate = bItem.ReportDate;
                tReportTube.SupplierId = bItem.SupplierId;
                tReportTube.SupplierName = bItem.SupplierName;
                tReportTube.TheaterName = bItem.TheaterName;
                tReportTube.NumberOfTicket = bItem.NumberOfTicket;
                tReportTube.TotalPurePrice = bItem.TotalPurePrice;
                tReportTube.NumberOfSoldTicket = bItem.NumberOfSoldTicket;
                tReportTube.TotalPrice = bItem.TotalPrice;
                if (tAdd == false) { tAdd = true; }
            }
            // else, update new one
            else
            {
                //update number of tiket and totalprice
                tReportTube.NumberOfTicket = tReportTube.NumberOfTicket + bItem.NumberOfTicket;
                tReportTube.TotalPurePrice = tReportTube.TotalPurePrice + bItem.TotalPurePrice;
                tReportTube.NumberOfSoldTicket = tReportTube.NumberOfSoldTicket + bItem.NumberOfSoldTicket;
                tReportTube.TotalPrice = tReportTube.TotalPrice + bItem.TotalPrice;
                if (tAdd == false) { tAdd = true; }
            }
            //=================================================================
            //caluculating balance in each tube
            priceBalance = bItem.TotalPrice - bItem.TotalPurePrice;
            numberBalance = bItem.NumberOfReturnTicket;//bItem.NumberOfSoldTicket -bItem.NumberOfTicket;
            totalBoughtNum = totalBoughtNum + bItem.NumberOfTicket;
            totalSoldNum = totalSoldNum + bItem.NumberOfSoldTicket;
            totalNumberBalance = totalNumberBalance + numberBalance;
            //===============================================================
            if (!TheatersReturnTicket.Keys.Contains(bItem.TheaterName))
            {
                TheatersReturnTicket.Add(bItem.TheaterName, -numberBalance);
            }
            else
            {
                TheatersReturnTicket[bItem.TheaterName] = TheatersReturnTicket[bItem.TheaterName] + (-numberBalance);
            }
            //===============================================================
            if (!ListSoldTheaters.Keys.Contains(bItem.TheaterId))
            {
                ListSoldTheaters.Add(bItem.TheaterId, bItem.TheaterName);
            }
        }
        TheaterSoldRank.Add(tReportTube);
        //============================================================================================
        foreach (var item in TheaterBalance)
        {
            realTotalRevenue = realTotalRevenue + item.TotalPrice;
            realBalance = item.TotalPrice - item.TotalPurePrice;
            realTotalBalance = realTotalBalance + realBalance;
        }
        //======================Present Mode 1 =================  
            
        <div class="tab-control" data-role="tab-control">
            <ul class="tabs">
                <li class="active"><a href="#overview" id="titletab4">TỔNG QUAN</a></li>
                <li><a href="#suppliertheaters" id="titletab5">THỐNG KÊ CÁC RẠP</a></li>
            </ul>

            <div class="frames">
                <div class="frame" id="overview">
                    <table class="table bordered hovered" style="width: 100%">
                        <tr style="background-color:teal; color: white">
                            <th>Tổng vé đã bán</th>
                            <th>Tổng vé thu hồi</th>
                            <th>Tổng doanh thu</th>
                        </tr>
                        <tr>
                            <td class="right">@totalSoldNum</td>
                            <td class="right">@(-totalNumberBalance)</td>
                            <td class="right">@realTotalRevenue.ToString("0,000")</td>
                        </tr>
                    </table>
                    <div>@*supplier's overview*@
                            <div style="display: block; width: 100%">
                                <div style="display: inline-block; width: 32%; vertical-align: top;">
                                    <table class="table bordered hovered" style="width: 100%;">
                                        <tr style="background-color:teal; color: white">
                                            <th colspan="3" style="font-weight: bold;">Tháng trong năm bán chạy nhất
                                            </th>
                                        </tr>
                                        <tr style="background-color: lightblue;">
                                            <th>STT
                                            </th>
                                            <th>Tháng
                                            </th>
                                            <th>Vé bán ra
                                            </th>
                                        </tr>
                                        @{int mRank = 0;
                                          List<ReportTube> sortedMonthRank = (
                                              from m in MonthRank orderby m.NumberOfSoldTicket descending select m).ToList();
                                        }
                                        @foreach (var n in sortedMonthRank)
                                        { 
                                            <tr>
                                                <td class="center">
                                                    @(mRank = mRank + 1)
                                                </td>
                                                <td class="center">
                                                    @n.ReportDate.Month
                                                </td>
                                                <td class="right">
                                                    @n.NumberOfSoldTicket
                                                </td>
                                            </tr>
                                        }
                                    </table>
                                </div>
                                <div style="display: inline-block; width: 32%; vertical-align: top;">
                                    <table class="table bordered hovered" style="width: 100%">
                                        <tr style="background-color:teal; color: white">
                                            <th colspan="3">Ngày trong tuần bán chạy nhất
                                            </th>
                                        </tr>
                                        <tr style="background-color: lightblue;">
                                            <th>STT
                                            </th>
                                            <th>Ngày
                                            </th>
                                            <th>Vé bán ra
                                            </th>
                                        </tr>
                                        @{int dRank = 0;
                                          List<ReportTube> sortedDayRank = (
                                              from m in DayRank orderby m.NumberOfSoldTicket descending select m).ToList();
                                        }
                                        @foreach (var n in sortedDayRank)
                                        { 
                                            <tr>
                                                <td class="center">
                                                    @(dRank = dRank + 1)
                                                </td>
                                                <td class="center">
                                                    @if (n.ReportDay == 0)
                                                    { 
                                                        @("Chủ nhật")
                                                    }
                                                    else
                                                    {
                                                        @("Thứ ")@(n.ReportDay + 1)
                                                    }
                                                </td>
                                                <td class="right">
                                                    @n.NumberOfSoldTicket
                                                </td>
                                            </tr>
                                        }
                                    </table>
                                </div>
                                <div style="display: inline-block; width: 32%; vertical-align: top;">
                                    <table class="table bordered hovered" style="width: 100%">
                                        <tr style="background-color:teal; color: white">
                                            <th colspan="3">Thể loại phim bán chạy nhất
                                            </th>
                                        </tr>
                                        <tr style="background-color: lightblue;">
                                            <th>STT
                                            </th>
                                            <th>Thể loại
                                            </th>
                                            <th>Vé bán ra
                                            </th>
                                        </tr>
                                        @{int cRank = 0;

                                          foreach (var n in cateData)
                                          {
                                              if (!CategoriesRanking.Keys.Contains(n.CategoryName))
                                              {
                                                  CategoriesRanking.Add(n.CategoryName, n.NumberOfSoldTicket);
                                              }
                                              else
                                              {
                                                  CategoriesRanking[n.CategoryName] = CategoriesRanking[n.CategoryName] + n.NumberOfSoldTicket;
                                              }
                                          }
                                          CategoriesRanking = CategoriesRanking.OrderByDescending(cr => cr.Value).ToDictionary(cr => cr.Key, cr => cr.Value);
                                        }
                                        @foreach (var cR in CategoriesRanking)
                                        {
                                            <tr>
                                                <td class="center">
                                                    @(cRank = cRank + 1)
                                                </td>
                                                <td>
                                                    @cR.Key
                                                </td>
                                                <td class="right">
                                                    @cR.Value
                                                </td>
                                            </tr> 
                                        }
                                    </table>
                                </div>
                            </div>
                    </div>
                            <div style="padding-left: 20px">
                                <input id="btnPrintTab4" class="info detail" type="submit" value="In báo cáo" />
                            </div>
                </div>
                <div class="frame" id="suppliertheaters">
                    <div>
                        @*Theater Statistic and balancing*@
                        <div class="unoverflow" style="max-height: 400px; overflow-y: scroll">
                            <table class="table bordered hovered" style=" width:100%">
                                <tr style="background-color:teal; color: white">
                                    <th style="width: 35%">Rạp</th>
                                    <th style="width: 15%">Vé bán ra</th>
                                    <th style="width: 20%">Doanh thu</th>
                                    <th style="width: 10%" class="detail"></th>
                                </tr>
                                @foreach (var item in ListSoldTheaters)
                                {
                                    int totalSoldTickets = 0;
                                    double totalTheaterBalance = 0;
                                    double totalTheaterSoldPrice = 0;
                                    List<ReportTube> sortedTheaterBalance = (
                                      from sb in TheaterBalance
                                      where sb.TheaterId == item.Key
                                      orderby sb.TheaterName, sb.ReportDate ascending
                                      select sb).ToList(); foreach (var sItem in sortedTheaterBalance)
                                    {
                                        double tBalance = 0;
                                        //caluculating balance in each tube
                                        tBalance = sItem.TotalPrice - sItem.TotalPurePrice;
                                        totalTheaterBalance = totalTheaterBalance + tBalance;
                                        totalSoldTickets = totalSoldTickets + sItem.NumberOfSoldTicket;
                                        totalTheaterSoldPrice = totalTheaterSoldPrice + sItem.TotalPrice;
                                    }
                                    <tr>
                                        <td>@item.Value</td>
                                        <td class="right">@totalSoldTickets</td>
                                        <td class="right">@totalTheaterSoldPrice.ToString("0,000")</td>
                                        <td class="detail">@Html.ActionLink("Chi tiết", "SupplierTheaterMonthsDetail", new {usn = @Session["UserId"], theaterId = @item.Key, fTime, tTime ,firstFromTime=fTime,firstToTime=tTime })</td>
                                    </tr>
                                }
                            </table>
                        </div>
                        <hr />
                        @*Xếp hạng theo thu nhập*@
                        <div style="width: 100%">
                            <div style="display: block; width: 100%">
                                <div style="display: inline-block; width: 32%; vertical-align: top;">
                                        <table class="table bordered hovered" style="width: 100%">
                                            <tr style="background-color:teal; color: white">
                                                <th colspan="3">Xếp hạng rạp theo số lượng vé bán
                                                </th>
                                            </tr>
                                            <tr style="background-color: lightblue;">
                                                <th>STT
                                                </th>
                                                <th>Rạp
                                                </th>
                                                <th>Vé bán ra
                                                </th>
                                            </tr>
                                            @{int spRank = 0;
                                              foreach (var m in TheaterSoldRank)
                                              {
                                                  if (!TheaterSoldNumberRank.Keys.Contains(m.TheaterName))
                                                  {
                                                      TheaterSoldNumberRank.Add(m.TheaterName, m.NumberOfSoldTicket);
                                                  }
                                                  else
                                                  {
                                                      TheaterSoldNumberRank[m.TheaterName] = TheaterSoldNumberRank[m.TheaterName] + m.NumberOfSoldTicket;
                                                  }
                                              }
                                              TheaterSoldNumberRank = TheaterSoldNumberRank.OrderByDescending(sn => sn.Value).ToDictionary(sn => sn.Key, sn => sn.Value);
                                            }
                                            @foreach (var m in TheaterSoldNumberRank)
                                            { 
                                                <tr>
                                                    <td class="center">
                                                        @(spRank = spRank + 1)
                                                    </td>
                                                    <td>
                                                        @m.Key
                                                    </td>
                                                    <td class="right">
                                                        @m.Value
                                                    </td>
                                                </tr>
                                            }
                                        </table>
                                </div>
                                <div style="display: inline-block; width: 32%; vertical-align: top;">
                                        <table class="table bordered hovered" style="width: 100%">
                                            <tr style="background-color:teal; color: white">
                                                <th colspan="3">Xếp hạng rạp theo doanh thu
                                                </th>
                                            </tr>
                                            <tr style="background-color: lightblue;">
                                                <th>STT
                                                </th>
                                                <th>Rạp
                                                </th>
                                                <th>Doanh thu
                                                </th>
                                            </tr>
                                            @{int eRank = 0;
                                              foreach (var n in TheaterSoldRank)
                                              {
                                                  if (!TheaterSoldPriceRank.Keys.Contains(n.TheaterName))
                                                  {
                                                      TheaterSoldPriceRank.Add(n.TheaterName, n.TotalPrice);
                                                  }
                                                  else
                                                  {
                                                      TheaterSoldPriceRank[n.TheaterName] = TheaterSoldPriceRank[n.TheaterName] + n.TotalPrice;
                                                  }
                                              }
                                              TheaterSoldPriceRank = TheaterSoldPriceRank.OrderByDescending(sn => sn.Value).ToDictionary(sn => sn.Key, sn => sn.Value);
                                            }
                                            @foreach (var n in TheaterSoldPriceRank)
                                            { 
                                                <tr>
                                                    <td class="center">
                                                        @(eRank = eRank + 1)
                                                    </td>
                                                    <td>
                                                        @n.Key
                                                    </td>
                                                    <td class="right">
                                                        @n.Value.ToString("0,000")
                                                    </td>
                                                </tr>
                                            }
                                        </table>
                                </div>
                                <div style="display: inline-block; width: 32%; vertical-align: top;">
                                        <table class="table bordered hovered" style="width: 100%">
                                            <tr style="background-color:teal; color: white">
                                                <th colspan="3">Xếp hạng rạp theo vé thu hồi
                                                </th>
                                            </tr>
                                            <tr style="background-color: lightblue;">
                                                <th>STT
                                                </th>
                                                <th>Rạp
                                                </th>
                                                <th>Vé thu hồi
                                                </th>
                                            </tr>
                                            @{int tRank = 0;
                                              TheatersReturnTicket = TheatersReturnTicket.OrderBy(rt => rt.Value).ToDictionary(rt => rt.Key, rt => rt.Value);
                                            }
                                            @foreach (var n in TheatersReturnTicket)
                                            { 
                                                <tr>
                                                    <td class="center">
                                                        @(tRank = tRank + 1)
                                                    </td>
                                                    <td>
                                                        @n.Key
                                                    </td>
                                                    <td class="right">
                                                        @n.Value
                                                    </td>
                                                </tr>
                                            }
                                        </table>
                                </div>
                            </div>
                        </div>
                    </div>
                            <div style="padding-left: 20px">
                                <input id="btnPrintTab5" class="info detail" type="submit" value="In báo cáo" />
                            </div>
                </div>
            </div>
        </div>   
    }
    else
    {
        <div class="tab-control" data-role="tab-control">
            <ul class="tabs">
                <li class="active"><a href="#sys">TỔNG QUAN HỆ THỐNG</a></li>
                <li><a href="#thea">THỐNG KÊ CÁC RẠP</a></li>
            </ul>

            <div class="frames">
                <div class="frame" id="sys" style="width: 100%">
                    <div style="text-align: center; font-weight: bold">
                        @{string message = null;
                          if (fDate > tDate)
                          {
                              message = "Ngày trong mục 'Từ ngày' không được lớn hơn ngày trong mục 'Đến ngày'";
                          }
                          else
                          {
                              message = "Không có dữ liệu giao dịch của hệ thống trong thời gian từ " + fDate.ToString("dd/MM/yyyy") + " đến " + tDate.ToString("dd/MM/yyyy");
                          }
                        }
                        @message
                    </div>
                </div>
                <div class="frame" id="thea" style="width: 100%">
                    <div style="text-align: center; font-weight: bold">
                        @{
                          if (fDate > tDate)
                          {
                              message = "Ngày trong mục 'Từ ngày' không được lớn hơn ngày trong mục 'Đến ngày'";
                          }
                          else
                          {
                              message = "Không có dữ liệu giao dịch của các rạp trong thời gian từ " + fDate.ToString("dd/MM/yyyy") + " đến " + tDate.ToString("dd/MM/yyyy");
                          }
                        }
                        @message
                    </div>
                </div>
            </div>
        </div>
    }
</div>
<div>
    @*<a href="~/Report/Index">Quay lại</a>*@    
    <div>
        <button class="button danger" style="float: right">
            @Html.ActionLink("Quay Lại", "/Index")
        </button>
    </div>
</div>

<script>
    $("#btnPrintTab4").click(function () {
        var titleText = '<h1>' + document.getElementById("ReportTitle").innerHTML + '</h1><hr \>';
        var tabText = '<h2>' + document.getElementById("titletab4").innerHTML + '</h2><br \>';
        var prtContent = document.getElementById("overview").innerHTML;

        var WinPrint = window.open('', '', 'letf=0,top=0,width=800,height=700');
        WinPrint.document.write('<html><head>');
        WinPrint.document.write('<link href="~/css/metro-bootstrap.css" rel="stylesheet" /><link href="~/css/own.css" rel="stylesheet" />');
        WinPrint.document.write('</head><body><div class="metro">');
        WinPrint.document.write('<style>.center {text-align: center;}.right {text-align: right;} .detail{display:none} .unoverflow{overflow-y:visible}');
        WinPrint.document.write('table,th,td{border:1px solid; border-color:gray;border-collapse:collapse; padding-bottom:10px;}</style>');
        WinPrint.document.write('<body><div>');
        WinPrint.document.write(titleText);
        WinPrint.document.write(tabText);
        WinPrint.document.write(prtContent);
        WinPrint.document.write('</div></body></html>');
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
        //WinPrint.close() 
    })
    $("#btnPrintTab5").click(function () {
        var titleText = '<h1>' + document.getElementById("ReportTitle").innerHTML + '</h1><hr \>';
        var tabText = '<h2>' + document.getElementById("titletab5").innerHTML + '</h2><br \>';
        var prtContent = document.getElementById("suppliertheaters").innerHTML;

        var WinPrint = window.open('', '', 'letf=0,top=0,width=800,height=700');
        WinPrint.document.write('<html><head>');
        WinPrint.document.write('<link href="~/css/metro-bootstrap.css" rel="stylesheet" /><link href="~/css/own.css" rel="stylesheet" />');
        WinPrint.document.write('</head><body><div class="metro">');
        WinPrint.document.write('<style>.center {text-align: center;}.right {text-align: right;} .detail{display:none} .unoverflow{overflow-y:visible}');
        WinPrint.document.write('table,th,td{border:1px solid; border-color:gray;border-collapse:collapse; padding-bottom:10px;}</style>');
        WinPrint.document.write('<body><div>');
        WinPrint.document.write(titleText);
        WinPrint.document.write(tabText);
        WinPrint.document.write(prtContent);
        WinPrint.document.write('</div></body></html>');
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
        //WinPrint.close() 
    })
</script>
